package com.yanqu.road.server.cross3server.cmd.activity.union;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;


@Cmd(code = Cross3Protocol.C3_CROSS_QUERY_USER_BASE_INFO, desc = "")
public class Cross3QueryUserReqCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ChannelTonBingProto.CTonBingGoGetUserBaseInfoReqMsg reqMsg = ChannelTonBingProto.CTonBingGoGetUserBaseInfoReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        List<Long> userIdList = reqMsg.getUserIdList();
        ChannelTonBingProto.CTonBingGoGetUserBaseInfoRespMsg.Builder respMsg = ChannelTonBingProto.CTonBingGoGetUserBaseInfoRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        for(long userId : userIdList){
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
            if(userBaseInfo != null){
                ChannelTonBingProto.CTonBingGoGetUserBaseInfoTempMsg.Builder tmpMsg = ChannelTonBingProto.CTonBingGoGetUserBaseInfoTempMsg.newBuilder();
                tmpMsg.setUserId(userId);
                tmpMsg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                respMsg.addUsers(tmpMsg);
            }
            if(respMsg.getUsersCount() > 20){
                MessageHelper.sendChannelServer(YanQuMessageUtils.buildMessage(ChannelProtocol.N_CHANNEL_QUERY_USER_RESP, respMsg));
                respMsg = ChannelTonBingProto.CTonBingGoGetUserBaseInfoRespMsg.newBuilder();
                respMsg.setActivityId(activityId);
            }
        }
        if(respMsg.getUsersCount() > 0){
            MessageHelper.sendChannelServer(YanQuMessageUtils.buildMessage(ChannelProtocol.N_CHANNEL_QUERY_USER_RESP, respMsg));
        }
    }

    @Override
    public void sendData() {

    }

}
